package root.electronclass.electronclass.grade

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import root.electronclass.electronclass.MainActivity
import root.electronclass.electronclass.R
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassBinding
import root.electronclass.electronclass.databinding.FragmentClassItemBinding
import root.electronclass.electronclass.ext.hideAll
import root.electronclass.electronclass.ext.show
import root.electronclass.electronclass.grade.attendance.ClassAttendanceFragment
import root.electronclass.electronclass.grade.curriculum.ClassCurriculumFragment
import root.electronclass.electronclass.grade.excellent_students.ClassExcellentStudentsFragment
import root.electronclass.electronclass.grade.homework.ClassHomeworkFragment
import root.electronclass.electronclass.grade.honor.ClassHonorFragment
import root.electronclass.electronclass.grade.member.ClassMemberFragment
import root.electronclass.electronclass.grade.notice.ClassNotificationFragment
import root.electronclass.electronclass.grade.record.ClassRecordFragment
import root.electronclass.electronclass.grade.rota.ClassRotaFragment

/**
 * A simple [Fragment] subclass.
 * create an instance of this fragment.
 */
class ClassFragment : ViewBindingFragment<FragmentClassBinding>(), MainActivity.IPage {

    private val items = linkedMapOf<String, Class<out Fragment>>(
        "班级驾驶舱" to ClassIndexFragment::class.java,
        "班级通知" to ClassNotificationFragment::class.java,
        "班级荣誉" to ClassHonorFragment::class.java,
        "班级风采" to ClassRecordFragment::class.java,
        "班级课表" to ClassCurriculumFragment::class.java,
        "值日表" to ClassRotaFragment::class.java,
        "班级座位" to ClassSeatFragment::class.java,//网页
        "班级考勤" to ClassAttendanceFragment::class.java,//网页
        "班级作业" to ClassHomeworkFragment::class.java,
        "班级成员墙" to ClassMemberFragment::class.java,
        "班级优秀生" to ClassExcellentStudentsFragment::class.java
    )
    private val icons = mapOf(
        "班级驾驶舱" to (R.drawable.ic_class_index to R.drawable.ic_class_index_text),
        "班级通知" to (R.drawable.ic_class_notification to R.drawable.ic_class_notification_text),
        "班级荣誉" to (R.drawable.ic_class_honor to R.drawable.ic_class_honor_text),
        "班级风采" to (R.drawable.ic_class_record to R.drawable.ic_class_record_text),
        "班级课表" to (R.drawable.ic_class_curriculum to R.drawable.ic_class_curriculum_text),
        "值日表" to (R.drawable.ic_class_rota to R.drawable.ic_class_rota_text),
        "班级座位" to (R.drawable.ic_class_seat to R.drawable.ic_class_seat_text),
        "班级考勤" to (R.drawable.ic_class_attendance to R.drawable.ic_class_attendance_text),
        "班级作业" to (R.drawable.ic_class_homework to R.drawable.ic_class_homework_text),
        "班级成员墙" to (R.drawable.ic_class_member to R.drawable.ic_class_member_text),
        "班级优秀生" to (R.drawable.ic_class_excellent_students to R.drawable.ic_class_excellent_students_text)
    )
    private var selectItem: String = items.keys.first()
    private var isInit = false
    private val adapter = BasicAdapter<String, FragmentClassItemBinding> { holder, item ->
        val icon = icons[item] ?: return@BasicAdapter
        holder.binding.itemIcon.setImageResource(icon.first)
        holder.binding.itemText.text = item
        val isSelected = item == selectItem
        holder.itemView.isSelected = isSelected
        holder.binding.itemText.layoutParams.width = if (isSelected) 150 else 120

        holder.itemView.setOnClickListener {
            changeTo(item)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        adapter.refresh(items.keys)
        binding.recycler.adapter = adapter
        isInit = true
        changeTo(selectItem)
    }

    private fun changeTo(item: String?) {
        val lastPosition = items.keys.indexOfFirst { selectItem == it }
        val adapterPosition = items.keys.indexOfFirst { item == it }
        selectItem = adapter.list[adapterPosition]
        adapter.notifyItemChanged(lastPosition)
        adapter.notifyItemChanged(adapterPosition)
        val clazz = items[item] ?: return
        childFragmentManager.show(R.id.class_container, clazz, childFragmentManager.hideAll())
    }

    override fun onPageSelected(bundle: Bundle?) {
        //选中当前页面
        val clazz: Class<*> = bundle?.getSerializable("TARGET_CLASS") as? Class<*> ?: ClassIndexFragment::class.java
        val index = items.values.toList().indexOf(clazz)
        if (isInit) {
            changeTo(items.keys.toList()[index])
        } else {
            selectItem = items.keys.toList()[index]
        }
    }
}